package com.seed.codegenerator.generator.system;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecgframework.codegenerate.database.DbReadTableUtil;
import org.jeecgframework.codegenerate.generate.impl.CodeGenerateOneToMany;
import org.jeecgframework.codegenerate.generate.pojo.ColumnVo;
import org.jeecgframework.codegenerate.generate.pojo.onetomany.MainTableVo;
import org.jeecgframework.codegenerate.generate.pojo.onetomany.SubTableVo;
import org.jeecgframework.codegenerate.generate.util.NonceUtils;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author gchiaway
 * 日期: 2021-01-11
 * 时间: 17:23
 */
@Slf4j
public class SystemCodeGenerateOneToMany extends CodeGenerateOneToMany {

    private final MainTableVo mainTableVo;
    private List<ColumnVo> mainColums;
    private List<ColumnVo> originalMainColumns;
    private final List<SubTableVo> subTables;
    private Map<String, String> extendParams;

    public SystemCodeGenerateOneToMany(MainTableVo mainTableVo, List<SubTableVo> subTables) {
        super(mainTableVo, subTables);
        this.mainTableVo = mainTableVo;
        this.subTables = subTables;
    }

    public SystemCodeGenerateOneToMany(MainTableVo mainTableVo, List<ColumnVo> mainColums, List<ColumnVo> originalMainColumns, List<SubTableVo> subTables) {
        super(mainTableVo, mainColums, originalMainColumns, subTables);
        this.mainTableVo = mainTableVo;
        this.mainColums = mainColums;
        this.originalMainColumns = originalMainColumns;
        this.subTables = subTables;
    }

    public SystemCodeGenerateOneToMany(MainTableVo mainTableVo, List<ColumnVo> mainColums, List<ColumnVo> originalMainColumns, List<SubTableVo> subTables, Map<String, String> extendParams) {
        super(mainTableVo, mainColums, originalMainColumns, subTables);
        this.mainTableVo = mainTableVo;
        this.mainColums = mainColums;
        this.originalMainColumns = originalMainColumns;
        this.subTables = subTables;
        this.extendParams = extendParams;
    }


    @Override
    public Map<String, Object> a() throws Exception {
        HashMap var1 = new HashMap();
        var1.put("bussiPackage", org.jeecgframework.codegenerate.a.a.g);
        var1.put("entityPackage", this.mainTableVo.getEntityPackage());
        var1.put("entityName", this.mainTableVo.getEntityName());
        var1.put("tableName", this.mainTableVo.getTableName());
        var1.put("ftl_description", this.mainTableVo.getFtlDescription());
        var1.put("primaryKeyField", org.jeecgframework.codegenerate.a.a.l);
        if (this.mainTableVo.getFieldRequiredNum() == null) {
            this.mainTableVo.setFieldRequiredNum(StringUtils.isNotEmpty(org.jeecgframework.codegenerate.a.a.m) ? Integer.parseInt(org.jeecgframework.codegenerate.a.a.m) : -1);
        }

        if (this.mainTableVo.getSearchFieldNum() == null) {
            this.mainTableVo.setSearchFieldNum(StringUtils.isNotEmpty(org.jeecgframework.codegenerate.a.a.n) ? Integer.parseInt(org.jeecgframework.codegenerate.a.a.n) : -1);
        }

        if (this.mainTableVo.getFieldRowNum() == null) {
            this.mainTableVo.setFieldRowNum(Integer.parseInt(org.jeecgframework.codegenerate.a.a.p));
        }

        var1.put("tableVo", this.mainTableVo);

        try {
            if (this.mainColums == null || this.mainColums.size() == 0) {
                this.mainColums = DbReadTableUtil.a(this.mainTableVo.getTableName());
            }

            if (this.originalMainColumns == null || this.originalMainColumns.size() == 0) {
                this.originalMainColumns = DbReadTableUtil.readOriginalTableColumn(this.mainTableVo.getTableName());
            }

            var1.put("columns", this.mainColums);
            var1.put("originalColumns", this.originalMainColumns);
            Iterator var2 = this.originalMainColumns.iterator();

            while(var2.hasNext()) {
                ColumnVo var3 = (ColumnVo)var2.next();
                if (var3.getFieldName().toLowerCase().equals(org.jeecgframework.codegenerate.a.a.l.toLowerCase())) {
                    var1.put("primaryKeyPolicy", var3.getFieldType());
                }
            }

            var2 = this.subTables.iterator();

            while(var2.hasNext()) {
                SubTableVo var12 = (SubTableVo)var2.next();
                List var4;
                if (var12.getColums() == null || var12.getColums().size() == 0) {
                    var4 = DbReadTableUtil.a(var12.getTableName());
                    var12.setColums(var4);
                }

                if (var12.getOriginalColumns() == null || var12.getOriginalColumns().size() == 0) {
                    var4 = DbReadTableUtil.readOriginalTableColumn(var12.getTableName());
                    var12.setOriginalColumns(var4);
                }

                String[] var13 = var12.getForeignKeys();
                ArrayList var5 = new ArrayList();
                String[] var6 = var13;
                int var7 = var13.length;

                for(int var8 = 0; var8 < var7; ++var8) {
                    String var9 = var6[var8];
                    var5.add(DbReadTableUtil.d(var9));
                }

                var12.setForeignKeys((String[])var5.toArray(new String[0]));
                var12.setOriginalForeignKeys(var13);
            }

            var1.put("subTables", this.subTables);
        } catch (Exception var10) {
            throw var10;
        }

        long var11 = NonceUtils.c() + NonceUtils.g();
        var1.put("serialVersionUID", String.valueOf(var11));
        if (!CollectionUtils.isEmpty(extendParams)) {
            var1.putAll(extendParams);
        }
        return var1;
    }

}
